package com.icebartech.core.utils;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.icebartech.core.constants.WeiXinConstant;
import com.icebartech.core.vo.ImageItem;
import com.icebartech.core.vo.MusicItem;
import com.icebartech.core.vo.NewsItem;
import com.icebartech.core.vo.TextItem;
import com.icebartech.core.vo.VideoItem;
import com.icebartech.core.vo.VoiceItem;


/**
 * 微信公众平台消息处理工具类
 * @author Administrator
 *
 */
public class WeiXinMessageUtil {
	/**
	 * 获取text类型回复消息的xml
	 * @param toUserName
	 * @param createTime
	 * @param content
	 * @return
	 */
	public static String getTextXml(String toUserName, String fromUserName, int createTime, TextItem item){
		
		if(StringUtils.isBlank(toUserName) || createTime <= 0 || StringUtils.isBlank(item.getContent())){
			return null;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		sb.append("<ToUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, toUserName)).append("</ToUserName>");
		sb.append("<FromUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, fromUserName)).append("</FromUserName>");
		sb.append("<CreateTime>").append(String.format(WeiXinConstant.CDATA_FORMAT, createTime)).append("</CreateTime>");
		sb.append("<MsgType>").append(String.format(WeiXinConstant.CDATA_FORMAT, WeiXinConstant.TEXT_TYPE)).append("</MsgType>");
		sb.append("<Content>").append(String.format(WeiXinConstant.CDATA_FORMAT, item.getContent())).append("</Content>");
		sb.append("</xml>");
		
		return sb.toString();
	}
	
	/**
	 * 获取image类型回复的xml
	 * @param toUserName
	 * @param creatTime
	 * @param mediaId
	 * @return
	 */
	public static String getImageXml(String toUserName, String fromUserName, int createTime, ImageItem item){
		if(StringUtils.isBlank(toUserName) || createTime <= 0 || StringUtils.isBlank(item.getMediaId())){
			return null;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		sb.append("<ToUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, toUserName)).append("</ToUserName>");
		sb.append("<FromUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, fromUserName)).append("</FromUserName>");
		sb.append("<CreateTime>").append(String.format(WeiXinConstant.CDATA_FORMAT, createTime)).append("</CreateTime>");
		sb.append("<MsgType>").append(String.format(WeiXinConstant.CDATA_FORMAT, WeiXinConstant.IMAGE_TYPE)).append("</MsgType>");
		sb.append("<Image>");
		sb.append("<MediaId>").append(String.format(WeiXinConstant.CDATA_FORMAT, item.getMediaId())).append("</MediaId>");
		sb.append("</Image>");
		sb.append("</xml>");
		
		return sb.toString();
	}
	
	/**
	 * 获取voice类型值的xml
	 * @param toUserName
	 * @param createTime
	 * @param mediaId
	 * @return
	 */
	public static String getVoiceXml(String toUserName, String fromUserName, int createTime, VoiceItem item){
		if(StringUtils.isBlank(toUserName) || createTime <= 0 || StringUtils.isBlank(item.getMediaId())){
			return null;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		sb.append("<ToUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, toUserName)).append("</ToUserName>");
		sb.append("<FromUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, fromUserName)).append("</FromUserName>");
		sb.append("<CreateTime>").append(String.format(WeiXinConstant.CDATA_FORMAT, createTime)).append("</CreateTime>");
		sb.append("<MsgType>").append(String.format(WeiXinConstant.CDATA_FORMAT, WeiXinConstant.VOICE_TYPE)).append("</MsgType>");
		sb.append("<Voice>");
		sb.append("<MediaId>").append(String.format(WeiXinConstant.CDATA_FORMAT, item.getMediaId())).append("</MediaId>");
		sb.append("</Voice>");
		sb.append("</xml>");
		
		return sb.toString();
	}
	
	/**
	 * 获取video类型的xml值
	 * @param toUserName
	 * @param createTime
	 * @param mediaId
	 * @return
	 */
	public static String getVideoXml(String toUserName, String fromUserName, int createTime, VideoItem item){
		if(StringUtils.isBlank(toUserName) || createTime <= 0
				|| item == null || StringUtils.isBlank(item.getMediaId())
				|| StringUtils.isBlank(item.getTitle())
				|| StringUtils.isBlank(item.getDescription())){
			return null;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		sb.append("<ToUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, toUserName)).append("</ToUserName>");
		sb.append("<FromUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, fromUserName)).append("</FromUserName>");
		sb.append("<CreateTime>").append(String.format(WeiXinConstant.CDATA_FORMAT, createTime)).append("</CreateTime>");
		sb.append("<MsgType>").append(String.format(WeiXinConstant.CDATA_FORMAT, WeiXinConstant.VOICE_TYPE)).append("</MsgType>");
		sb.append("<Video>");
		sb.append("<MediaId>").append(String.format(WeiXinConstant.CDATA_FORMAT, item.getMediaId())).append("</MediaId>");
		sb.append("<Title>").append(String.format(WeiXinConstant.CDATA_FORMAT, item.getTitle())).append("</Title>");
		sb.append("<Description>").append(String.format(WeiXinConstant.CDATA_FORMAT, item.getDescription())).append("</Description>");
		sb.append("</Video>");
		sb.append("</xml>");
		
		return sb.toString();
	}
	
	/**
	 * 获取music类型的xml值
	 * @param toUserName
	 * @param createTime
	 * @param title
	 * @param description
	 * @param musicUrl
	 * @param hqMusicUrl
	 * @param thumbMediaId
	 * @return
	 */
	public static String getMusicXml(String toUserName, String fromUserName, int createTime, MusicItem item){
		if(StringUtils.isBlank(toUserName) 
				|| createTime <= 0 
				|| item == null
				|| StringUtils.isBlank(item.getTitle()) 
				|| StringUtils.isBlank(item.getThumbMediaId())
				|| StringUtils.isBlank(item.getDescription())
				|| StringUtils.isBlank(item.getMusicUrl())
				|| StringUtils.isBlank(item.getHQMusicUrl())){
			return null;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		sb.append("<ToUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, toUserName)).append("</ToUserName>");
		sb.append("<FromUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, fromUserName)).append("</FromUserName>");
		sb.append("<CreateTime>").append(String.format(WeiXinConstant.CDATA_FORMAT, createTime)).append("</CreateTime>");
		sb.append("<MsgType>").append(String.format(WeiXinConstant.CDATA_FORMAT, WeiXinConstant.MUSIC_TYPE)).append("</MsgType>");
		sb.append("<Music>");
		sb.append("<Title>").append(String.format(WeiXinConstant.CDATA_FORMAT, item.getTitle())).append("</Title>");
		sb.append("<Description>").append(String.format(WeiXinConstant.CDATA_FORMAT, item.getDescription())).append("</Description>");
		sb.append("<MusicUrl>").append(String.format(WeiXinConstant.CDATA_FORMAT, item.getMusicUrl())).append("</MusicUrl>");
		sb.append("<HQMusicUrl>").append(String.format(WeiXinConstant.CDATA_FORMAT, item.getHQMusicUrl())).append("</HQMusicUrl>");
		sb.append("<ThumbMediaId>").append(String.format(WeiXinConstant.CDATA_FORMAT, item.getThumbMediaId())).append("</ThumbMediaId>");
		sb.append("</Music>");
		sb.append("</xml>");
		
		return sb.toString();
	}
	
	/**
	 * 获取图文类型的xml
	 * @param toUserName
	 * @param createTime
	 * @param newsList
	 * @return
	 */
	public static String getNewsXml(String toUserName, String fromUserName, int createTime, List<NewsItem> newsList){
		if(StringUtils.isBlank(toUserName) || createTime <= 0 || CollectionUtils.isEmpty(newsList)){
			return null;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		sb.append("<ToUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, toUserName)).append("</ToUserName>");
		sb.append("<FromUserName>").append(String.format(WeiXinConstant.CDATA_FORMAT, fromUserName)).append("</FromUserName>");
		sb.append("<CreateTime>").append(String.format(WeiXinConstant.CDATA_FORMAT, createTime)).append("</CreateTime>");
		sb.append("<MsgType>").append(String.format(WeiXinConstant.CDATA_FORMAT, WeiXinConstant.NEWS_TYPE)).append("</MsgType>");
		sb.append("<ArticleCount>").append(String.format(WeiXinConstant.CDATA_FORMAT, newsList.size())).append("</ArticleCount>");
		sb.append("<Articles>");
		for(NewsItem newsItem : newsList){
			sb.append("<item>");
			sb.append("<Title>").append(String.format(WeiXinConstant.CDATA_FORMAT, newsItem.getTitle())).append("</Title>");
			sb.append("<Description>").append(String.format(WeiXinConstant.CDATA_FORMAT, newsItem.getDescription())).append("</Description>");
			sb.append("<PicUrl>").append(String.format(WeiXinConstant.CDATA_FORMAT, newsItem.getPicUrl())).append("</PicUrl>");
			sb.append("<Url>").append(String.format(WeiXinConstant.CDATA_FORMAT, newsItem.getUrl())).append("</Url>");
			sb.append("</item>");
		}
		sb.append("</Articles>");
		sb.append("</xml>");
		
		return sb.toString();
	}
	
	/**
	 * 构造微信支付中传递的xml，可通用
	 * @param paramMap
	 * @return
	 */
	public static String getWeiXinPayXml(Map<String, String> paramMap){
		StringBuffer sb = new StringBuffer();
		if(paramMap != null && paramMap.size() > 0){
			sb.append("<xml>");
			for(Entry<String, String> entry : paramMap.entrySet()){
				sb.append("<" + entry.getKey() + ">");
				sb.append(String.format(WeiXinConstant.CDATA_FORMAT, entry.getValue()));
				sb.append("</" + entry.getKey() + ">");
			}
			sb.append("</xml>");
			return sb.toString();
		}else{
			return null;
		}
	}
}
